//
//  CZEmoticonPageCell.swift
//  表情键盘新工程-74.准备
//
//  Created by itcast on 16/5/21.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

// 一个页按钮个数 20
let CZEmoticonNumberOfPage = 20

// 一页有多少列 7
let CZEmoticonNumberOfColumn = 7

// 一页有多少行 3
let CZEmoticonNumberOfRow = 3

protocol CZEmoticonPageCellDelegate: NSObjectProtocol {
    // 点击删除按钮
    func emoticonPageCellDidClickDeleteButton()
    
    // 点击表情按钮
    func emoticonPageCellDidClickEmoticonButton(emoticonModel: CZEmoticonModel)
}

/// collectionView的cell,cell里面会有21个按钮
class CZEmoticonPageCell: UICollectionViewCell {
    
    // 代理属性
    weak var delegate: CZEmoticonPageCellDelegate?
    
    /// cell一页需要的模型
    var pageEmoticon: [CZEmoticonModel]? {
        didSet {
            // 拿到cell需要的一页数据
            
            // 当一页模型只有2个的时候,只会遍历2次.只有2个按钮会设置模型,其他按钮什么都不干,这些是从其他页复用过来的,什么都不干,这些按钮会显示之前非内容,需要把不显示内容的按钮隐藏掉
            // 隐藏cell所有的表情按钮
            for button in emoticonButtons {
                button.hidden = true
            }
            
            // 遍历 获取 一个模型和对应的按钮
            var index = 0
            for emoticonModel in pageEmoticon! {
                // 获取到这个模型对应的按钮
                let button = emoticonButtons[index]
                
                // 按钮显示模型数据
                button.emoticonModel = emoticonModel
                
                // 需要显示模型的按钮显示出来
                button.hidden = false
                
                index++
            }
        }
    }
    
    // indexPath
    var indexPath: NSIndexPath? {
        didSet {
            // 设置调试信息
            debugLabel.text = "第 \(indexPath!.section) 组, 第 \(indexPath!.item) 个 cell"
            
            recentLabel.hidden = (indexPath!.section != 0)
        }
    }
    
    /// 20个表情按钮,方便后面使用,遍历
    private var emoticonButtons = [CZEmoticonButton]()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI() {
        // 添加20个表情按钮
        addEmoticonButtons()
        
        contentView.addSubview(recentLabel)
        
        // 添加删除按钮
        contentView.addSubview(deleteButton)
        
        // 添加调试label
        contentView.addSubview(debugLabel)
    }
    
    /// 添加20个表情按钮
    private func addEmoticonButtons() {
        for _ in 0..<CZEmoticonNumberOfPage {
            let button = CZEmoticonButton()
//            button.backgroundColor = UIColor.randomColor()
            
            button.addTarget(self, action: "didClickEmoticonButton:", forControlEvents: UIControlEvents.TouchUpInside)
            
            // 设置字体
            button.titleLabel?.font = UIFont.systemFontOfSize(32)
            
            // 添加到cell里面
            contentView.addSubview(button)
            
            // 添加到表情数组,方便后面遍历
            emoticonButtons.append(button)
        }
    }
    
    // 在layoutSubView里面设置frame
    override func layoutSubviews() {
        super.layoutSubviews()
        // 父控件的大小确定,还有通过autoLayout约束的view的大小也确定了
        
        // 按钮布局的倡廉
        let marginLR = 5
        let marginB = 25
        
        // 计算所有表情按钮的frame
        // 一个按钮的宽度 = (cell.width - 2 * marginLR) / CZEmoticonNumberOfColumn
        let width = (self.bounds.width - 2 * CGFloat(marginLR)) / CGFloat(CZEmoticonNumberOfColumn)
        // 一个按钮的高度 = (cell.height - marginB) / CZEmoticonNumberOfRow
        let height = (self.bounds.height - CGFloat(marginB)) / CGFloat(CZEmoticonNumberOfRow)
        
        var index = 0
        for button in emoticonButtons {
            
            // 计算按钮在哪行
            let row = index / CZEmoticonNumberOfColumn
            
            // 计算按钮在哪列
            let column = index % CZEmoticonNumberOfColumn
            
            let x = CGFloat(marginLR) + CGFloat(column) * width
            
            let y = CGFloat(row) * height
            
            let frame = CGRect(x: x, y: y, width: width, height: height)
            button.frame = frame
            
            index++
        }
        
        // 计算删除按钮的frame
        // 删除按钮的列数和行数是固定的
        let deleteX = CGFloat(marginLR) + CGFloat(CZEmoticonNumberOfColumn - 1) * width
        let deleteY = CGFloat(CZEmoticonNumberOfRow - 1) * height
        let deleteFrame = CGRect(x: deleteX, y: deleteY, width: width, height: height)
        deleteButton.frame = deleteFrame
        
        // 最近

        recentLabel.center = CGPoint(x: self.frame.width * 0.5, y: self.frame.height - recentLabel.frame.height)
    }
    
    // MARK: - 点击事件
    @objc private func didClickDeleteButton() {
        delegate?.emoticonPageCellDidClickDeleteButton()
    }
    
    @objc private func didClickEmoticonButton(emoticonButton: CZEmoticonButton) {
        delegate?.emoticonPageCellDidClickEmoticonButton(emoticonButton.emoticonModel!)
    }
    
    // MARK: - 懒加载
    /// 调试label
    private lazy var debugLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.redColor()
        label.font = UIFont.systemFontOfSize(30)
        label.frame = CGRect(x: 0, y: 30, width: 300, height: 100)
        label.hidden = true
        
        return label
    }()
    
    /// 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        
        // 设置图片
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: "didClickDeleteButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    /// 最近label
    private lazy var recentLabel: UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(12)
        label.textColor = UIColor.lightGrayColor()
        label.text = "最近使用表情"
        label.sizeToFit()
        
        return label
    }()
}
